# 门禁功能
import threading, time, random
def door():
    open_time = 0
    while True:
        if event.isSet():
            open_time += 1  # 计算门开着的时间
            time.sleep(1)  # 必须要有延时,否则,可能员工还没进去,这边循环又到了3,clear,门重新关上了
        else:
            open_time = 0
            event.wait()  # 门关着等待
        if open_time > 3:
            event.clear()  # 3秒之后关门
            print("门重新关上")
def employee(name):
    while True:
        if event.isSet():
            print("门开了,%s进入公司"%name)
            break
        else:
            print("门关着,%s刷卡"%name)
            event.set()
        time.sleep(0.5)  # 每次进门或者刷卡时间
if __name__ == "__main__":
    event = threading.Event()
    door = threading.Thread(target=door)
    door.start()
    for i in range(5):
        e = threading.Thread(target=employee, args=("第%s个人" % str(i + 1),))
        time.sleep(random.randrange(3))  # 员工陆续进门
        e.start()
